#!/bin/bash
# leebor
# 2017-11-1
# install-git
# v1.0

# 相关依赖包
Rel_Pack=(curl-devel expat-devel gettext-devel openssl-devel zlib-devel texinfo openjade perl perl-XML-SAX)

# git下载地址
Git_Url="https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz"

# 文件下载目录
Down_Dir="/data/downloads"

if [ ! -d "/data/downloads" ];then
    mkdir -p /data/downloads
fi

# 安装依赖
function Install_Rel() {
    for Pack in ${Rel_Pack[@]};do
	    if [ "$(rpm -qa | grep $Pack)"x  = ""x ];then
		    yum install -y $Pack
		fi
	done
}


# 编译安装git（不包含html doc等）
function Install_git() {
    if [ ! -f "$Down_Dir/git-2.9.5.tar.gz" ];then
        echo "start Wget down"
        wget -P $Down_Dir $Git_Url
    fi
    echo "unzip file"
    tar -zxf $Down_Dir/git-2.9.5.tar.gz -C /tmp/
    if [ ! -d '/tmp/git-2.9.5' ];then
        echo ""
        exit
    else
	cd /tmp/git-2.9.5
	make configure
	if [ "$?" != 0 ];then echo ">>>>>> Error";exit;fi
	./configure --prefix=/opt/git-2.9.2
	if [ "$?" != 0 ];then echo ">>>>>> Error ";exit;fi
	make 
	if [ "$?" != 0 ];then echo ">>>>>> Error ";exit;fi
	make install 
	if [ "$?" != 0 ];then echo ">>>>>> Error ";exit;fi
	ln -s /opt/git-2.9.2 /opt/git
    fi
}


# 添加到系统环境变量
function Change_source() {
    if [ -f "/etc/profile" ];then
        echo "Change profile"
	Git='#Add by Script '$0'\nGIT_HOME=/opt/git\nPATH=$GIT_HOME/bin:$PATH'
	sed -i "/^export/i$Git" /etc/profile
	source /etc/profile
	git --version
    else
	echo ">>>>>> Error"
    fi
	
}

Install_Rel
if [ $? -eq 0 ];then
    echo ">>>>>> start install git "
    Install_git
	if [ $? -eq 0 ];then
	    Change_source
            echo ">>>>>> Change source"
	fi
else
    echo "Error in rel install"
fi